<template>
  <div class="app-container" v-show="showCaseModel">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主诉" prop="chiefComplaint">
        <el-input
          v-model="queryParams.chiefComplaint"
          placeholder="请输入主诉"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="症状" prop="historyOfPresentIllness">
        <el-input
          v-model="queryParams.historyOfPresentIllness"
          placeholder="请输入症状"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="历史治疗" prop="historyOfTreatment">
        <el-input
          v-model="queryParams.historyOfTreatment"
          placeholder="请输入历史治疗"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="既往史" prop="pastHistory">
        <el-input
          v-model="queryParams.pastHistory"
          placeholder="请输入既往史"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="过敏史" prop="allergies">
        <el-input
          v-model="queryParams.allergies"
          placeholder="请输入过敏史"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健康检查" prop="healthCheckup">
        <el-input
          v-model="queryParams.healthCheckup"
          placeholder="请输入健康检查"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初步诊断Id串" prop="priliminaryDiseIdList">
        <el-input
          v-model="queryParams.priliminaryDiseIdList"
          placeholder="请输入初步诊断Id串"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="初诊诊断id串对应诊断名字串" prop="priliminaryDiseStrList">
        <el-input
          v-model="queryParams.priliminaryDiseStrList"
          placeholder="请输入初诊诊断id串对应诊断名字串"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病例模板名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入病例模板名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dms:dms_case_model:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dms:dms_case_model:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dms:dms_case_model:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dms:dms_case_model:export']"
        >导出</el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row> -->

    <!-- 不显示表格
      <el-table v-loading="loading" :data="dms_case_modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板id" align="center" prop="id" />
      <el-table-column label="主诉" align="center" prop="chiefComplaint" />
      <el-table-column label="症状" align="center" prop="historyOfPresentIllness" />
      <el-table-column label="历史治疗" align="center" prop="historyOfTreatment" />
      <el-table-column label="既往史" align="center" prop="pastHistory" />
      <el-table-column label="过敏史" align="center" prop="allergies" />
      <el-table-column label="健康检查" align="center" prop="healthCheckup" />
      <el-table-column label="初步诊断Id串" align="center" prop="priliminaryDiseIdList" />
      <el-table-column label="初诊诊断id串对应诊断名字串" align="center" prop="priliminaryDiseStrList" />
      <el-table-column label="病例模板名" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dms:dms_case_model:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:dms_case_model:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改病例模版对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body> -->
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      :inline="true"
    >
      <el-form-item label="主诉" prop="chiefComplaint">
        <el-input v-model="form.chiefComplaint" placeholder="请输入主诉" />
      </el-form-item>
      <el-form-item label="症状" prop="historyOfPresentIllness">
        <el-input
          v-model="form.historyOfPresentIllness"
          placeholder="请输入症状"
        />
      </el-form-item>
      <el-form-item label="历史治疗" prop="historyOfTreatment">
        <el-input
          v-model="form.historyOfTreatment"
          placeholder="请输入历史治疗"
        />
      </el-form-item>
      <el-form-item label="既往史" prop="pastHistory">
        <el-input v-model="form.pastHistory" placeholder="请输入既往史" />
      </el-form-item>
      <el-form-item label="过敏史" prop="allergies">
        <el-input v-model="form.allergies" placeholder="请输入过敏史" />
      </el-form-item>
      <el-form-item label="健康检查" prop="healthCheckup">
        <el-input v-model="form.healthCheckup" placeholder="请输入健康检查" />
      </el-form-item>
      <!-- <el-form-item label="初步诊断Id串" prop="priliminaryDiseIdList">
          <el-input v-model="form.priliminaryDiseIdList" placeholder="请输入初步诊断Id串" />
        </el-form-item>
        <el-form-item label="初诊诊断id串对应诊断名字串" prop="priliminaryDiseStrList">
          <el-input v-model="form.priliminaryDiseStrList" placeholder="请输入初诊诊断id串对应诊断名字串" />
        </el-form-item> -->
      <el-form-item label="病例模板名" prop="name">
        <el-input v-model="form.name" placeholder="请输入病例模板名" />
      </el-form-item>
      <!-- <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item> -->
    </el-form>
    <el-tag>评估诊断:</el-tag>
    <el-card>
      <el-button type="text" style="float:right" @click="showDiseList"
        >添加诊断</el-button
      >
      <el-table :data="diseList">
        <el-table-column label="ICD编码" prop="icd"></el-table-column>
        <el-table-column label="名称" prop="name"></el-table-column>
        <el-table-column label="编码" prop="code"></el-table-column>
        <el-table-column width="80">
          <template slot-scope="scope">
            <el-button type="text" @click="deleteDis(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <DiseList
      ref="DiseList"
      @diseListCallback="updateDiseList"
      :selectedDiseList="diseList"
    ></DiseList>
    <el-button  style="margin-top:10px;" type="primary" size="mini" @click="saveCaseModel"
      >保存修改</el-button>
    <!-- </el-dialog> -->
  </div>
</template>

<script>
import {
  listDms_case_model,
  getDms_case_model,
  delDms_case_model,
  addDms_case_model,
  updateDms_case_model,
  exportDms_case_model
} from "@/api/dms/dms_case_model";
import { getDise } from "@/api/dms/dise";
// 导入疾病列表组件
import DiseList from "@/components/Dms/Dise/";

export default {
  name: "Dms_case_model",
  // 接收父组件传参
  props: ["caseModelId", "showCaseModel"],
  // 注册子组件
  components: { DiseList },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 病例模版表格数据
      dms_case_modelList: [],
      // 疾病列表
      diseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chiefComplaint: null,
        historyOfPresentIllness: null,
        historyOfTreatment: null,
        pastHistory: null,
        allergies: null,
        healthCheckup: null,
        priliminaryDiseIdList: null,
        priliminaryDiseStrList: null,
        name: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    // this.getList();
    // this.handleLoad();
  },
  methods: {
    /** 保存修改 */
    saveCaseModel() {
      
      if (this.diseList) {
        this.form.priliminaryDiseIdList = this.diseList.map(item => item.id).join(",");
        this.form.priliminaryDiseStrList = this.diseList.map(item => item.name).join(",");
      }
      this.submitForm();
    },
    /** 页面加载 */
    handleLoad(caseModelId) {
      this.reset();
      const id = caseModelId;
      getDms_case_model(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改病例模版";
        // 根据疾病id查询疾病列表
        this.diseList.splice(0, this.diseList.length);
        if (response.data.priliminaryDiseIdList) {
          let diseIds = response.data.priliminaryDiseIdList.split(",");
          diseIds.forEach(element => {
            getDise(element).then(response => {
              this.diseList.push(response.data);
            });
          });
        }
      });
    },
    // 显示疾病列表组件
    showDiseList() {
      this.$refs.DiseList.addDise();
    },
    // 添加疾病
    updateDiseList(data) {
      this.diseList.push(data);
      this.$message({
        message: "添加成功",
        type: "success"
      });
    },
    // 删除疾病
    deleteDis(data) {
      this.$confirm("确定要删除该疾病吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.diseList.splice(
            this.diseList.findIndex(item => item.id === data.id),
            1
          );
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    /** 查询病例模版列表 */
    getList() {
      this.loading = true;
      listDms_case_model(this.queryParams).then(response => {
        this.dms_case_modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chiefComplaint: null,
        historyOfPresentIllness: null,
        historyOfTreatment: null,
        pastHistory: null,
        allergies: null,
        healthCheckup: null,
        priliminaryDiseIdList: null,
        priliminaryDiseStrList: null,
        name: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加病例模版";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDms_case_model(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改病例模版";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDms_case_model(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDms_case_model(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除病例模版编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delDms_case_model(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有病例模版数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportDms_case_model(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>